<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">

<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type"
        content="text/css">
  <meta http-equiv="Content-Script-Type"
        content="text/javascript">
  <meta name="description"
        content="FreeType Documentation - Glyph Conventions - Glyph
                 Metrics">
  <meta name="Author"
        content="David Turner">

  <link rel="icon"
        href="../image/favicon_-90.ico">
  <link rel="shortcut icon"
        href="../image/favicon_-90.ico">
  <link rel="stylesheet"
        type="text/css"
        href="../css/freetype2_-90.css">

  <script type="text/javascript"
          src="../../../js/freetype2.js">
  </script>

  <title>FreeType Glyph Conventions | Glyph Metrics</title>
</head>


<body>

<div id="top"
     class="bar">
  <h1><a href="http://freetype.org/index.html">FreeType</a> Glyph
    Conventions&nbsp;/&nbsp;III</h1>
</div>


<div id="wrapper">

<div class="colmask leftmenu">
  <div class="colright">
    <div class="col1wrap">
      <div class="col1">


        <!-- ************************************************** -->

	<div id="glyph-metrics">
	  <h2>III. Glyph Metrics</h2>

	  <h3 id="section-1">1. Baseline, pens and layouts</h3>

	  <p>The baseline is an imaginary line that is used to
	    &lsquo;guide&rsquo; glyphs when rendering text.  It can be
	    horizontal (e.g., Latin, Cyrillic, Arabic) or vertical
	    (e.g., Chinese, Japanese, Mongolian).  Moreover, to render
	    text, a virtual point, located on the baseline, called
	    the <em>pen position</em> or <em>origin</em>, is used to
	    locate glyphs.</p>

	  <p>Each layout uses a different convention for glyph
	  placement:</p>

	  <ul>
	    <li>
              <p>With horizontal layout, glyphs simply
		&lsquo;rest&rsquo; on the baseline.  Text is rendered
		by incrementing the pen position, either to the right
		or to the left.</p>

              <p>The distance between two successive pen positions is
		glyph-specific and is called the <em>advance
		width</em>.  Note that its value is <em>always</em>
		positive, even for right-to-left oriented scripts like
		Arabic.  This introduces some differences in the way
		text is rendered.</p>

              <p><em>The pen position is always placed on the
		  baseline.</em></p>

              <p align="center">
		<img src="layout.png"
		     height="179"
		     width="458"
		     alt="horizontal layout">
	      </p>
	    </li>
	    <li>
              <p>With a vertical layout, glyphs are centered around
		the baseline:</p>

              <p align="center">
		<img src="layout2.png"
		     height="275"
		     width="162"
		     alt="vertical layout">
              </p>
	    </li>
	  </ul>


	  <h3 id="section-2">2. Typographic metrics and bounding
	    boxes</h3>

	  <p>A various number of face metrics are defined for all
	    glyphs in a given font.</p>

	  <ul>
	    <li class="emph">
              <p>Ascent</p>

              <p>The distance from the baseline to the highest or
		upper grid coordinate used to place an outline point.
		It is a positive value, due to the grid's orientation
		with the <i>Y</i>&nbsp;axis upwards.</p>
	    </li>
	    <li class="emph">
              <p>Descent</p>

              <p>The distance from the baseline to the lowest grid
		coordinate used to place an outline point.  In
		FreeType, this is a negative value, due to the grid's
		orientation.  Note that in some font formats this is a
		positive value.</p>
	    </li>
	    <li class="emph">
              <p>Linegap</p>

              <p>The distance that must be placed between two lines of
		text.  The baseline-to-baseline distance should be
		computed as

	      <p align="center">
		<code>linespace = ascent - descent + linegap</code>
	      </p>

              <p>if you use the typographic values.</p>
	    </li>
	  </ul>

	  <p>Other, simpler metrics are:</p>

	  <ul>
	    <li class="emph">
              <p>Bounding box</p>

              <p>This is an imaginary box that encloses all glyphs
		from the font, usually as tightly as possible.  It is
		represented by four parameters,
		namely <code>xMin</code>, <code>yMin</code>,
                <code>xMax</code>, and <code>yMax</code>, that can be
		computed for any outline.  Their values can be in font
		units if measured in the original outline, or in
		integer (or fractional) pixel units when measured on
		scaled outlines.</p>

	      <p>A common shorthand for the bounding box is
		&lsquo;bbox&rsquo;.</p>
	    </li>
	    <li class="emph">
              <p>Internal leading</p>

              <p>This concept comes directly from the world of traditional
		typography.  It represents the amount of space within the
		<em>leading</em> which is reserved for glyph features
		that lay outside of the EM square (like accentuation).
		It usually can be computed as</p>

              <p align="center">
		<code>internal leading = ascent - descent - EM_size</code>
              </p>
	    </li>
	    <li class="emph">
              <p>External leading</p>

              <p>This is another name for the line gap.</p>
	    </li>
	  </ul>


	  <h3 id="section-3">3. Bearings and Advances</h3>

	  <p>Each glyph has also distances called <em>bearings</em> and
	    <em>advances</em>.  The actual values depend on the
	    layout, as the same glyph can be used to render text
	    either horizontally or vertically:</p>

	  <ul>
	    <li class="emph">
              <p>Left side bearing</p>

              <p>The horizontal distance from the current pen position
		to the glyph's left bbox edge.  It is positive for
		horizontal layouts, and in most cases negative for
		vertical ones.</p>

              <p>In the FreeType API, this is also called
		<code>bearingX</code>.  Another shorthand is
		&lsquo;lsb&rsquo;.</p>
	    </li>
	    <li class="emph">
              <p>Top side bearing</p>
	
              <p>The vertical distance from the baseline to the top of
		the glyph's bbox.  It is usually positive for
		horizontal layouts, and negative for vertical
		ones.</p>

              <p>In the FreeType API, this is also called
		<code>bearingY</code>.</p>
	    </li>
	    <li class="emph">
              <p>Advance width</p>

              <p>The horizontal distance to increment (for
		left-to-right writing) or decrement (for right-to-left
		writing) the pen position after a glyph has been
		rendered when processing text.  It is always positive
		for horizontal layouts, and zero for vertical
		ones.</p>

              <p>In the FreeType API, this is also called
		<code>advanceX</code>.</p>
	    </li>
	    <li class="emph">
              <p>Advance height</p>

              <p>The vertical distance to decrement the pen position
		after a glyph has been rendered.  It is always zero
		for horizontal layouts, and positive for vertical
		layouts.</p>
	
              <p>In the FreeType API, this is also called
		<code>advanceY</code>.</p>
	    </li>
	    <li class="emph">
              <p>Glyph width</p>

              <p>The glyph's horizontal extent.  For unscaled font
		coordinates, it is</p>

	      <p align="center">
		<code>glyph width = bbox.xMax - bbox.xMin</code>
	      </p>

	      <p>For scaled glyphs, its computation requests specific
		care, described in the grid-fitting chapter below.</p>
	    </li>
	    <li class="emph">
              <p>Glyph height</p>

              <p>The glyph's vertical extent. For unscaled font
		coordinates, it is</p>

	      <p align="center">
		<code>glyph height = bbox.yMax - bbox.yMin</code>
	      </p>

	      <p>For scaled glyphs, its computation requests specific
		care, described in the grid-fitting chapter below.</p>
	    </li>
	    <li class="emph">
              <p>Right side bearing</p>

              <p>Only used for horizontal layouts to describe the
		distance from the bbox's right edge to the advance
		width.  In most cases it is a non-negative number:</p>

              <p align="center">
		<tt>right side bearing = advance_width -
		  left_side_bearing - (xMax-xMin)</tt>
              </p>

	      <p>A common shorthand for this value is
		&lsquo;rsb&rsquo;.</p>
	    </li>
	  </ul>

	  <p>Here is a picture giving all the details for horizontal metrics:

	  <p align="center">
	    <img src="metrics.png"
		 height="253"
		 width="388"
		 alt="horizontal glyph metrics">
	  </p>

	  <p>And here is another one for the vertical metrics:</p>

	  <p align="center">
	    <img src="metrics2.png"
		 height="278"
		 width="294"
		 alt="vertical glyph metrics">
	  </p>


	  <h3 id="section-4">4. The effects of grid-fitting</h3>

	  <p>Because hinting aligns the glyph's control points to the
	    pixel grid, this process slightly modifies the dimensions
	    of character images in ways that differ from simple
	    scaling.</p>

	  <p>For example, the image of the lowercase &lsquo;m&rsquo;
	    letter sometimes fits a square in the master grid.
	    However, to make it readable at small pixel sizes, hinting
	    tends to enlarge its scaled outline horizontally in order
	    to keep its three legs distinctly visible, resulting in a
	    wider character bitmap.</p>

	  <p>The glyph metrics are also influenced by the grid-fitting
	    process:</p>

	  <ul>
	    <li>
              <p>The image's width and height are altered.  Even if
		this is only by one pixel, it can make a big
		difference at small pixel sizes.</p>
	    </li>
	    <li>
              <p>The image's bounding box is modified, thus modifying
		the bearings.</p>
	    </li>
	    <li>
              <p>The advances must be updated.  For example, the
		advance width must be incremented if the hinted bitmap
		is larger than the scaled one, to reflect the
		augmented glyph width.</p>
	    </li>
	  </ul>

	  <p>This has some implications:</p>

	  <ul>
	    <li>
              <p>Because of hinting, simply scaling the font ascent or
		descent might not give correct results.  A possible
		solution is to keep the ceiling of the scaled ascent,
		and floor of the scaled descent.</p>
	    </li>
	    <li>
              <p>There is no easy way to get the hinted glyph and
		advance widths of a range of glyphs, as hinting works
		differently on each outline.  The only solution is to
		hint each glyph separately and record the returned
		values (for example in a cache).  Some formats, like
		TrueType, even include a table of pre-computed values
		for a small set of common character pixel sizes.</p>
	    </li>
	    <li>
              <p>Hinting depends on the final character width and
		height in pixels, which means that it is highly
		resolution-dependent.  This property makes correct
		WYSIWYG layouts difficult to implement.</p>
	    </li>
	  </ul>

	  <p>Performing 2D transformations on glyph outlines is very
	    easy with FreeType.  However, when using translation on
	    hinted outlines, one should always take care
	    of <b>exclusively using integer pixel distances</b> (which
	    means that the parameters to the
	    <code>FT_Outline_Translate</code> API function should all
	    be multiples of&nbsp;64, as the point coordinates are in
	    26.6&nbsp;fixed-point format).  Otherwise, the translation
	    will simply <em>ruin the hinter's work</em>, resulting in
	    very low quality bitmaps!</p>

          <p>Note, however, that the restriction to integer pixel
            distances mentioned in the previous paragraph has become
            weaker; today, it is quite common to do <em>no</em>
            hinting along the horizontal axis, only adjusting the
            glyphs vertically.  Typical examples are Microsoft's
            ClearType implementation, FreeType's new CFF engine
            (contributed by Adobe), or the &lsquo;light&rsquo;
            auto-hinting mode.  For such modes you get best rendering
            results if you do sub-pixel glyph positioning.</p>


	  <h3 id="section-5">5. Text widths and bounding box</h3>

	  <p>As seen before, the &lsquo;origin&rsquo; of a given glyph
	    corresponds to the position of the pen on the baseline.
	    It is not necessarily located on one of the glyph's
	    bounding box corners, unlike many typical bitmapped font
	    formats.  In some cases, the origin can be out of the
	    bounding box, in others, it can be within it, depending on
	    the shape of the given glyph.</p>

	  <p>Likewise, the glyph's &lsquo;advance width&rsquo; is the
	    increment to apply to the pen position during layout, and
	    is not related to the glyph's &lsquo;width&rsquo;, which
	    really is the glyph's bounding box width.</p>

	  <p>The same conventions apply to strings of text, with the
	  following consequences.</p>

	  <ul>
	    <li>
              <p>The bounding box of a given string of text doesn't
		necessarily contain the text cursor, nor is the latter
		located on one of its corners.</p>
	    </li>
	    <li>
              <p>The string's advance width isn't related to its
		bounding box dimensions.  Especially if it contains
		leading and trailing spaces or tabs.</p>
	    </li>
	    <li>
              <p>Finally, additional processing like kerning creates
		strings of text whose dimensions are not directly
		related to the simple juxtaposition of individual
		glyph metrics.  For example, the advance width of
		&lsquo;VA&rsquo; isn't the sum of the advances of
		&lsquo;V&rsquo; and &lsquo;A&rsquo; taken
		separately.</p>
	    </li>
	  </ul>
        </div>

        <!-- ************************************************** -->

        <div class="updated">
          <p>Last update: 13-Feb-2018</p>
        </div>
      </div>
    </div>


    <!-- ************************************************** -->

    <div class="col2">
    </div>
  </div>
</div>


<!-- ************************************************** -->

<div id="TOC">
  <ul>
    <li class="funding">
      <form action="https://www.paypal.com/cgi-bin/webscr"
            method="post"
            target="_top">
        <input type="hidden"
               name="cmd"
               value="_s-xclick">
        <input type="hidden"
               name="hosted_button_id"
               value="SK827YKEALMT4">
        <input type="submit"
               value="Donate"
               name="submit"
               alt="PayPal - The safer, easier way to pay online!">
      </form>
    </li>
    <li class="primary">
      <a href="http://freetype.org/index.html">Home</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/index.html#news">News</a>
    </li>
    <li class="primary">
      <a href="../index.html">Overview</a>
    </li>
    <li class="primary">
      <a href="../documentation.html">Documentation</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/developer.html">Development</a>
    </li>
    <li class="primary">
      <a href="http://freetype.org/contact.html"
         class="emphasis">Contact</a>
    </li>

    <li>
      &nbsp; <!-- separate primary from secondary entries -->
    </li>

    <li class="secondary">
      <a href="index.html">FreeType Glyph Conventions</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-1.html">Basic Typographic Concepts</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-2.html">Glyph Outlines</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-3.html" class="current">Glyph Metrics</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-4.html">Kerning</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-5.html">Text Processing</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-6.html">FreeType Outlines</a>
    </li>
    <li class="tertiary">
      <a href="glyphs-7.html">FreeType Bitmaps</a>
    </li>
  </ul>
</div>

</div> <!-- id="wrapper" -->

<div id="TOC-bottom">
</div>

</body>
</html>
